import http from '@/common/http';
import { Dialog } from 'mand-mobile';
import Loading from '@/components/loading';
const defaultState = () => {
  return {
    hitchOrderDetailInfo: null,
    hitchInvitationOrderInfo: null,
    sendInvitationOrder: null,
    hitchSubmitPaymentInfo: null,
    hitchPartySubmitPaymentInfo: null
  };
};
const state = defaultState();
const actions = {
  // 只用来通过接口或者异步处理
  getHitchOrderDetailInfo(context, orderNo) {
    return new Promise((resolve, reject) => {
      http
        .post('/pakj/hitchOrder/getHitchOrder', {
          orderNo: orderNo
        })
        .then(res => {
          if (res.data.code === '000000') {
            context.commit('setHitchOrderDetaiInfo', res.data.data);
            resolve();
          } else {
            Dialog.alert({
              title: '错误',
              content: res.data.msg,
              confirmText: '确定'
            });
            reject(new Error(res.data.code + ':' + res.data.msg));
          }
        });
    });
  },
  // 获取邀请车主接单详情
  getHitchInvitationOrderInfo(context, parm) {
    return new Promise((resolve, reject) => {
      http.post('/pakj/hitchOrder/getDriverAllot', parm).then(res => {
        if (res.data.code === '000000') {
          context.commit('setHitchInvitationOrderInfo', res.data.data);
          resolve();
        } else {
          Dialog.alert({
            title: '错误',
            content: res.data.msg,
            confirmText: '确定'
          });
          reject(new Error(res.data.code + ':' + res.data.msg));
        }
      });
    });
  },
  // 发送邀请车主接单
  sendInvitationOrderInfo(context, parm) {
    return new Promise((resolve, reject) => {
      http.post('/pakj/hitchOrder/sendOrderInvitation', parm).then(res => {
        if (res.data.code === '000000') {
          context.commit('setSendInvitationOrderInfo', res.data.data);
          resolve();
        } else {
          Dialog.alert({
            title: '错误',
            content: res.data.msg,
            confirmText: '确定'
          });
          reject(new Error(res.data.code + ':' + res.data.msg));
        }
      });
    });
  },
  // 余额支付
  hitchSubmitPayment(context, param) {
    return new Promise((resolve, reject) => {
      http.post('/pakj/hitchOrder/balancePayOrder', param).then(res => {
        if (res.data.code === '000000') {
          context.commit('setHitchSubmitPaymentInfo', res.data.data);
          resolve();
        } else {
          Dialog.alert({
            title: '错误',
            content: res.data.msg,
            confirmText: '确定'
          });
          reject(new Error(res.data.code + ':' + res.data.msg));
        }
      });
    });
  },
  // 第三方支付
  hitchPartySubmitPayment(context, param) {
    return new Promise((resolve, reject) => {
      http.post('/pakj/hitchOrder/payHitchOrder', param).then(res => {
        if (res.data.code === '000000') {
          context.commit('setHitchPartySubmitPaymentInfo', res.data.data);
          resolve();
        } else {
          Dialog.alert({
            title: '错误',
            content: res.data.msg,
            confirmText: '确定'
          });
          reject(new Error(res.data.code + ':' + res.data.msg));
        }
      });
    });
  },
  // 微信支付成功后 查询订单支付状态
  hitchPaymentStatus(context, params) {
    return new Promise((resolve, reject) => {
      Loading.show();
      http.post('/pakj/hitchOrder/getOrderPayStatus', params).then(res => {
        Loading.hide();
        if (res.data.code === '000000') {
          if (res.data.data.code === '1') {
            resolve();
          } else if (res.data.data.code === '2') {
            // 订单未支付
            reject(new Error(res.data.data.code + ':' + res.data.data.msg));
          } else if (res.data.data.code === '5') {
            // 等待支付
            reject(new Error(res.data.data.code + ':' + res.data.data.msg));
          } else if (res.data.data.code === '4') {
            // 支付失败
            reject(new Error(res.data.data.code + ':' + res.data.data.msg));
          }
        }
      });
    });
  }
};
const mutations = {
  // 修改state的数据。同步操作
  setHitchOrderDetaiInfo(state, data) {
    console.log('得到的订单信息:', data);
    state.hitchOrderDetailInfo = data;
  },
  setHitchInvitationOrderInfo(state, data) {
    console.log('获取邀请车主行程详情:', data);
    state.hitchInvitationOrderInfo = data;
  },
  setSendInvitationOrderInfo(state, data) {
    console.log('发送邀请车主接单:', data);
    state.sendInvitationOrder = data;
  },
  setHitchSubmitPaymentInfo(state, data) {
    console.log('余额支付:', data);
    state.hitchSubmitPaymentInfo = data;
  },
  setHitchPartySubmitPaymentInfo(state, data) {
    console.log('第三方支付:', data);
    state.hitchPartySubmitPaymentInfo = data;
  }
};
const getters = {
  getHitchOrderDetailData(state) {
    return state.hitchOrderDetailInfo;
  },
  getHitchInvitationOrderData(state) {
    return state.hitchInvitationOrderInfo;
  },
  getInvitationOrderData(state) {
    return state.sendInvitationOrder;
  },
  getHitchSubmitPaymentInfoData(state) {
    return state.hitchSubmitPaymentInfo;
  },
  getHitchPartySubmitPaymentData(state) {
    return state.hitchPartySubmitPaymentInfo;
  }
};
export default {
  state,
  actions,
  mutations,
  getters
};
